﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBQLTV;

namespace DAO
{
    public class PhieuNhapSachDAO
    {
        public bool InsertPhieuNhapSach(string NgayNhap, int TongTien)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                //Kiểm tra xem đã có chưa
                DateTime ngayNhap=Convert.ToDateTime(NgayNhap);
                PHIEUNHAPSACH phieuNhap_temp = entity.PHIEUNHAPSACHes.FirstOrDefault(o => o.NgayNhap.Equals(ngayNhap));
                if (phieuNhap_temp != null)
                {
                    phieuNhap_temp.TongTien += TongTien;
                }
                else
                {
                    PHIEUNHAPSACH phieuNhap = new PHIEUNHAPSACH()
                    {
                        NgayNhap = Convert.ToDateTime(NgayNhap),
                        TongTien = TongTien
                    };
                    entity.PHIEUNHAPSACHes.AddObject(phieuNhap);
                }
                entity.SaveChanges();
                return true;
            }
        }
        public List<PHIEUNHAPSACH> GetListPhieuNhapSach()
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                List<PHIEUNHAPSACH> list = entity.PHIEUNHAPSACHes.ToList();
                return list;
            }
        }
        public PHIEUNHAPSACH GetPhieuNhapSach(string NgayNhap)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                DateTime ngNhap=Convert.ToDateTime(NgayNhap);
                PHIEUNHAPSACH phieu = entity.PHIEUNHAPSACHes.FirstOrDefault(o => o.NgayNhap.Equals(ngNhap));
                return phieu;
            }
        }
    }
}
